import * as Api from "../../api/device";

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    kf_icon: "../../images/icon-servie-gray.png",
    toastImg: "../../images/toast-ok.png",
    rules: {
      charging_mode: "time",
      deposit: "",
      day_price: "",
      device_lang_dist: [],
      free_device_num: 0,
      time_price_rules: [],
    },
    maxPrice: "",
    checked: false,
    cabinet_number: "",
    num: 1,
    visibleDialog: false,
    isShowToast: false,
    toastText: "",
    language: {},
    device_lang_dist: [],
    custom_back: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad(options) {
    console.log("options____________", options);
    // const scene = options.scene;
    const scene = options.scene
      ? options.scene
      : decodeURIComponent(options.q).split("scene=")[1];
    console.log("cabinetinfo_options", scene);

    console.log("app.globalData.wxScene", app.globalData.wxScene);
    console.log("wx.getStorageSync('wxScene')", wx.getStorageSync("wxScene"));
    const wxScene = app.globalData.wxScene;
    if (
      wxScene === 1047 ||
      wxScene === 1048 ||
      wxScene === 1049 ||
      wxScene === 1011 ||
      wxScene === 1012 ||
      wxScene === 1013 ||
      wxScene === 1001
    ) {
      const is_login =
        wxScene === 1047 || wxScene === 1048 || wxScene === 1049
          ? app.globalData.is_login
          : wx.getStorageSync("is_login");
      this.setData({ isShowToast: null });
      console.log("app.globalData.is_login", app.globalData.is_login);
      console.log("app.globalData.param", app.globalData.param);
      console.log(
        'wx.getStorageSync("is_login")',
        wx.getStorageSync("is_login")
      );

      if (is_login) {
        this.setData({
          custom_back: false,
          cabinet_number: app.globalData.param ? app.globalData.param : scene,
        });
        this._getCabinetInfo();
        console.log("this.data.custom_back", this.data.custom_back);
      } else {
        wx.navigateTo({
          url: "/pages/index/index?scene=" + scene,
        });
        // wx.navigateTo({
        //   url: "/pages/login/login",
        // });
        // this.setData({
        //   custom_back: true,
        //   cabinet_number: options.scene,
        // });
        // this._getCabinetInfo();
      }
    } else {
      console.log("111111111");
      this.setData({
        cabinet_number: scene,
      });

      this._getCabinetInfo();
    }
  },
  handleBackEvent() {
    // 获取页面栈
    const pages = getCurrentPages();
    // 获取上一页实例
    const prevPage = pages[pages.length - 2];
    // 修改上一页的数据
    prevPage.setData({
      show: true,
    });
    wx.navigateBack();
  },

  /**
   * @description: 获取设备信息
   */
  _getCabinetInfo() {
    Api.getCabinetInfo(this.data.cabinet_number)
      .then((res) => {
        console.log("res", res);
        this.setData({
          rules: res,
          maxPrice:
            res.time_price_rules.length > 0
              ? res.time_price_rules[res.time_price_rules.length - 1].price
              : 0,
          device_lang_dist: res.device_lang_dist,
          language: res.device_lang_dist[0],
          isShowToast: false,
        });
        wx.removeStorageSync("wxScene");
        app.globalData.wxScene = "";
      })
      .catch((err) => {
        this.setData({
          isShowToast: true,
          toastText: err.msg,
        });
      });
  },

  /**
   * @description: 租借数量
   */
  onChangeStepper(e) {
    this.setData({ num: e.detail });
  },
  /**
   * @description: 同意协议
   */
  onChangeAgreement(e) {
    this.setData({ checked: !this.data.checked });
  },
  handlePolicy() {
    wx.navigateTo({
      url: `/pages/webview/webview?link=${app.globalData.agreement}`,
    });
  },
  handleKf() {
    app.handleKf();
  },
  /**
   * @description: 支付订单
   * @return {*}
   */
  handleSubmit() {
    if (!this.data.checked)
      return wx.showToast({ title: "请先同意协议", icon: "none" });

    const { cabinet_number, num, language } = this.data;
    Api.createDepositOrder({
      cabinet_number,
      num,
      lang_code: language.code,
    })
      .then((result) => {
        if (result.is_paid) {
          wx.navigateTo({
            url:
              "/pages/hireSuccess/hireSuccess?order_number=" +
              result.order_number,
          });
        } else {
          wx.requestPayment({
            ...result.pay_info,

            success(res) {
              wx.showLoading({
                title: "加载中",
              });

              setTimeout(
                function () {
                  wx.hideLoading();
                  // 支付成功后的处理逻辑
                  wx.navigateTo({
                    url:
                      "/pages/hireSuccess/hireSuccess?order_number=" +
                      result.order_number,
                  });
                },
								// num > 2 ? 10000 : 5000
								1000
              );
            },
            fail(err) {
              wx.showToast({
                title: "支付失败,请重试",
                icon: "error",
              });
              // 支付失败的处理逻辑
            },
          });
        }
      })
      .catch((err) => {
        wx.showToast({
          title: err.msg,
        });
      });
  },
  /**
   * @description: 显示语言选择弹窗
   */
  onChangeLanguage() {
    this.setData({
      visibleDialog: true,
    });
  },
  onClose() {
    this.setData({
      visibleDialog: false,
    });
  },
  onChangePicker(e) {
    console.log("e", e);

    this.setData({
      language: e.detail.value,
      visibleDialog: false,
      num: 1,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},
  onUnload() {
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]; // 获取上一个页面实例对象
    if (prevPage) {
      prevPage.setData({ show: true }); // 设置上一个页面的 show 为 true
    }
  },
});
